﻿#include "InCircleGraphicsItem.h"
#include "GraphicsDef.h"
#include <QGraphicsScene>
#include <QGraphicsSceneMouseEvent>
#include <QPainter>

namespace OfUI {

	InCircleGraphicsItem::InCircleGraphicsItem(QGraphicsItem *parent)
		: EllipseGraphicsItem(parent)
	{
		setType(ItemType::INSCRIBED_CIRCLE);
	}

	InCircleGraphicsItem::~InCircleGraphicsItem()
	{
	}

	BaseGraphicsItem::MouseDragType InCircleGraphicsItem::select(const QPointF & point, int * selType) const
	{
		MouseDragType eDragType = DRAG_NONE;
		SelectType eSelectType = SELECT_NONE;
		QMap<SelectType, QPointF> dragPointMap = getDragSelectPoint(m_rect);

		for (auto iter = dragPointMap.begin(); iter != dragPointMap.end(); ++iter)
		{
			double dDistance = ((iter.key() == SELECT_CENTER) ? 5 : 3) / getViewScale();
			QPointF dragPoint = iter.value();
			QRectF pointRect(dragPoint - QPointF(dDistance, dDistance), dragPoint + QPointF(dDistance, dDistance));

			if (pointRect.contains(point))
			{
				eDragType = DRAG_EDIT;
				eSelectType = iter.key();
				break;
			}
		}

		if (SELECT_NONE == eSelectType)
		{
			// 判断鼠标在内切圆内部
			if (QLineF(point, m_rect.center()).length() <= qAbs(m_rect.width()) / 2)
			{
				eDragType = DRAG_MOVE;
				eSelectType = SELECT_ALL;
			}
		}

		if (selType)
		{
			*selType = eSelectType;
		}
		return eDragType;
	}

	void InCircleGraphicsItem::mouseMoveEvent(QGraphicsSceneMouseEvent * event)
	{
		if (SELECT_NONE == m_eSelType)
			return;

		QPointF mousePos = event->scenePos();
		QRectF sceneRect = scene()->sceneRect();
		// 保证鼠标在场景内
		mousePos.setX(qMin(mousePos.rx(), sceneRect.right()));
		mousePos.setY(qMin(mousePos.ry(), sceneRect.bottom()));
		mousePos.setX(qMax(mousePos.rx(), sceneRect.left()));
		mousePos.setY(qMax(mousePos.ry(), sceneRect.top()));

		QRectF newRect = m_rect;

		if (SELECT_TOP == m_eSelType)
		{
			QPointF bottomCenter(newRect.center().x(), newRect.bottom());
			QPointF topCenter(newRect.center().x(), mousePos.y());
			double dCricleRadius = (bottomCenter.y() - topCenter.y()) / 2;
			newRect = QRectF(topCenter - QPointF(dCricleRadius, 0), bottomCenter + QPointF(dCricleRadius, 0));
		}
		else if (SELECT_LEFT == m_eSelType)
		{
			QPointF leftCenter(mousePos.x(), newRect.center().y());
			QPointF rightCenter(newRect.right(), newRect.center().y());
			double dCricleRadius = (rightCenter.x() - leftCenter.x()) / 2;
			newRect = QRectF(leftCenter - QPointF(0, dCricleRadius), rightCenter + QPointF(0, dCricleRadius));
		}
		else if (SELECT_RIGHT == m_eSelType)
		{
			QPointF leftCenter(newRect.left(), newRect.center().y());
			QPointF rightCenter(mousePos.x(), newRect.center().y());
			double dCricleRadius = (rightCenter.x() - leftCenter.x()) / 2;
			newRect = QRectF(leftCenter - QPointF(0, dCricleRadius), rightCenter + QPointF(0, dCricleRadius));
		}
		else if (SELECT_BOTTOM == m_eSelType)
		{
			QPointF bottomCenter(newRect.center().x(), mousePos.y());
			QPointF topCenter(newRect.center().x(), newRect.top());
			double dCricleRadius = (bottomCenter.y() - topCenter.y()) / 2;
			newRect = QRectF(topCenter - QPointF(dCricleRadius, 0), bottomCenter + QPointF(dCricleRadius, 0));
		}
		else if (SELECT_TOP_LEFT == m_eSelType)
		{
			QRectF rt = getSquareFromRect(QRectF(newRect.bottomRight(), mousePos));
			newRect.setTopLeft(rt.bottomRight());
		}
		else if (SELECT_TOP_RIGHT == m_eSelType)
		{
			QRectF rt = getSquareFromRect(QRectF(newRect.bottomLeft(), mousePos));
			newRect.setTopRight(rt.bottomRight());
		}
		else if (SELECT_BOTTOM_LEFT == m_eSelType)
		{
			QRectF rt = getSquareFromRect(QRectF(newRect.topRight(), mousePos));
			newRect.setBottomLeft(rt.bottomRight());
		}
		else if (SELECT_BOTTOM_RIGHT == m_eSelType)
		{
			QRectF rt = getSquareFromRect(QRectF(newRect.topLeft(), mousePos));
			newRect.setBottomRight(rt.bottomRight());
		}
		else if (SELECT_CENTER == m_eSelType || SELECT_ALL == m_eSelType)
		{
			newRect.translate(mousePos - m_pressMousePos);
			m_pressMousePos = mousePos;
		}
		setRect(newRect);
	}

}
